<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Wyp
 */

namespace App\Modules\Live\Controllers;

use App\Modules\Live\Services\WxLiveService;
use App\Modules\Live\Services\TLSSigAPIv2;
use App\Modules\Live\Validators\WxLiveControllerValidator;
use Flower\Core\Http\BaseController;
use Illuminate\Http\Request;
use App\Lib\Facades\FLAuth;

class WxLiveController extends BaseController
{

    private $liveService;

    public function __construct(WxLiveService $liveService)
    {
        $this->liveService = $liveService;
    }

    public function getRules()
    {
        return WxLiveControllerValidator::validator();
    }

    /**
     * 获取用户昵称
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function getMemberNick(Request $request) {
        $member_id = $request->input('member_id');
        $data = $this->liveService->getMemberInfo($member_id);
        return $this->display($data, '查询成功');
    }

    /**
     * 用户进入直播间
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function addLiveMember(Request $request)
    {
        $member_id = FLAuth::id();
        $live_id = $request->input('live_id');
        $live_member = $this->liveService->addLiveMember($member_id,$live_id);

        $data = [
            'id' => $live_member->id
        ];

        return $this->display($data, '创建成功');
    }

    /**
     * 退出直播间
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function outLiveMember(Request $request)
    {
        $member_id = FLAuth::id();
        $live_id = $request->input('live_id');
        $this->liveService->editLiveMember($member_id,$live_id);

        return $this->success('修改成功');
    }

    /**
     * 获取在线用户列表
     * @param Request $request
     * @return mixed
     * @throws \Flower\Core\Repository\Exceptions\RepositoryException
     */
    public function getLiveMemberListPage(Request $request)
    {

        $live_id = $request->input('live_id');
        $pageSize = $request->input('pageSize',10);
        $member_list = $this->liveService->getLiveMemberListPage($live_id,$pageSize);

        return $this->display($member_list, '返回成功');
    }
    /**
     * 获取直播信息
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     * @throws \Flower\Core\Repository\Exceptions\RepositoryException
     */
    public function getRoomInfo(Request $request) {
        $live_id = $request->input('live_id');
        $data = $this->liveService->getRoomInfo($live_id);
        return $this->display($data, '查询成功');
    }

    /**
     * 获取直播相关参数
     * @param Request $request
     * @return mixed
     */
    public function getLiveMessage(Request $request) {
        $live_id = $request->input('live_id');
        $data = $this->liveService->getLiveMessage($live_id);
        return $this->display($data, '查询成功');
    }

    /**
     * 获取分享二维码
     * @param Request $request
     * @return mixed
     */
    public function getQrcode(Request $request) {
        $page = $request->input('page');
        $scene = $request->input('live_id');
        $data = $this->liveService->getQrcode($page,$scene);
        return $this->display($data, '查询成功');
    }

    /**
     * 点赞
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function createLiveLike(Request $request)
    {
        $member_id = FLAuth::id();
        $live_id = $request->input('live_id');
        $live_like = $this->liveService->addLiveLike($member_id,$live_id);

        $data = [
            'id' => $live_like->id
        ];

        return $this->display($data, '点赞成功');
    }

    /**
     * 取消点赞（已弃用）
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function updateLiveLike(Request $request)
    {
        $member_id = FLAuth::id();
        $live_id = $request->input('live_id');
        $live_like = $this->liveService->updateLiveLike($member_id,$live_id);

        $data = [
            'id' => $live_like->id
        ];

        return $this->display($data, '操作成功');
    }

    /**
     * 关注
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function createLiveAttention(Request $request)
    {
        $member_id = FLAuth::id();
        $user_id = $request->input('user_id');
        $live_attention = $this->liveService->addLiveAttention($member_id,$user_id);

        $data = [
            'id' => $live_attention->id
        ];

        return $this->display($data, '关注成功');
    }

    /**
     * 取消关注
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function updateLiveAttention(Request $request)
    {
        $member_id = FLAuth::id();
        $user_id = $request->input('user_id');
        $live_attention = $this->liveService->updateLiveAttention($member_id,$user_id);

        $data = [
            'id' => $live_attention->id
        ];

        return $this->display($data, '操作成功');
    }

    /**
     * 小程序获取腾讯签名
     * @return mixed
     * @throws \Exception
     */
    public function getWxSig() {
        $user_id = FLAuth::id();
        $api = new TLSSigAPIv2(env('SDK_APP_ID'),env('PRIVATE_KEY_STRING'));
        $sig = $api->genSig('member'.$user_id);//生成usersig
        $data = [
            'sig' => $sig,
            'sdkappid' => env('SDK_APP_ID'),
            'member_id' => FLAuth::id()
        ];
        return $this->display($data, '获取成功');
    }

    /**
     * 获取直播列表
     * @param Request $request
     * @return mixed
     * @throws \Flower\Core\Repository\Exceptions\RepositoryException
     */
    public function getLiveList(Request $request) {
        $pageSize = $request->input('pageSize',10);
        $page = $request->input('page',1);
        $lon = $request->input('lon');
        $lat = $request->input('lat');
        $distance = $request->input('distance');
        $live_list = $this->liveService->getLiveList($lon,$lat,$distance,$pageSize,$page);

        return $this->display($live_list, '返回成功');
    }
}
